package com.bocai.dahuatech.ui.activity;

import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.widget.EditText;

import com.bocai.dahuatech.Bean.BaseBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseActivity;
import com.bocai.dahuatech.utils.AESUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 重置密码
 */
public class SetNewPsdActivity extends BaseActivity {
    @Bind(R.id.et_setnewpsd_psdtrue)
    EditText etpsdtrue;
    @Bind(R.id.et_setnewpsd_psd)
    EditText etpsd;
    String phone;
    @Override
    protected int contentView() {
        return R.layout.activity_setnewpsd;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        initToolbar(R.string.resetpsd);
        phone= getIntent().getStringExtra("phone");
        mToolbar.inflateMenu(R.menu.menu_regist_third);
        initEvent();
    }

    @Override
    protected void initEvent() {
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                resetpsd();
                return false;
            }
        });
    }

    /**
     * 重置密码
     */
    private void resetpsd() {
        if(TextUtils.isEmpty(etpsd.getText().toString())){
            showToast(R.string.psdnotnull);

        }
        else if(etpsd.getText().length()<6)
        {
            showToast(R.string.psdnot6);
        }else if(TextUtils.isEmpty(etpsdtrue.getText().toString())){
            showToast(R.string.psdtruenotnull);
        }
        else if(!etpsd.getText().toString().equals(etpsdtrue.getText().toString()))
        {
            showToast(R.string.psdfalse);
        }else
        {
            getTestApi().resetPwd(phone, AESUtils.md5(etpsdtrue.getText().toString()))
                    .compose(this.<BaseBean>bindToLifecycle())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<BaseBean>() {

                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {
                            Log.e("error", e.toString());
                        }

                        @Override
                        public void onNext(BaseBean baseBean) {
                            Log.e("result", baseBean.getReturnInfo());
                            showToast(baseBean.getReturnInfo());
                            if (baseBean.getReturnNo().equals("1")) {
                                finish();
                            }
                        }
                    });
        }
    }
}
